<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将 callable 转换为闭包</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="closure.call.html">« Closure::call</a></li>
      <li style="float: right;"><a href="class.stdclass.html">stdClass »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.closure.html">Closure</a></li>
    <li>将 callable 转换为闭包</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="closure.fromcallable" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Closure::fromCallable</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.1.0)</p><p class="refpurpose"><span class="refname">Closure::fromCallable</span> &mdash; <span class="dc-title">将 callable 转换为闭包</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-closure.fromcallable-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><strong>Closure::fromCallable</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>): <span class="type"><a href="class.closure.html" class="type Closure">Closure</a></span></div>

  <p class="para rdfs-comment">
   使用当前范围从给定的 <code class="parameter">callback</code> 创建并返回一个新的 
   <a href="functions.anonymous.html" class="link">匿名函数</a>。 
   此方法检查 <code class="parameter">callback</code> 函数在作用域是否可调用，
   如果不能，就抛出 <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span>。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    从 PHP 8.1.0 开始，<a href="functions.first_class_callable_syntax.html" class="link">First-class 可调用语法</a> 的语义与此方法相同。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-closure.fromcallable-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">callback</code></dt>

    <dd>

     <p class="para">
      要转换的回调。
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-closure.fromcallable-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回新创建的 <span class="classname"><a href="class.closure.html" class="classname">Closure</a></span>，
   或者如果 <code class="parameter">callback</code> 在当前作用域无法调用，
   则抛出 <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span>。
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-closure.fromcallable-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><a href="functions.anonymous.html" class="link">匿名函数</a></li>
   <li class="member"><a href="functions.first_class_callable_syntax.html" class="link">First-class 可调用语法</a></li>
  </ul>
 </div>


</div></div></div></body></html>